/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package br.com.gerenciador_recursos.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Brian Wing Shun Chan
 */
public class ParamUtil {

	public static String[] BOOLEANS = {"true", "t", "y", "on", "1"};

	public static final boolean DEFAULT_BOOLEAN = false;

	public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];

	public static final byte DEFAULT_BYTE = 0;

	public static final byte[] DEFAULT_BYTE_VALUES = new byte[0];

	public static final Date[] DEFAULT_DATE_VALUES = new Date[0];

	public static final double DEFAULT_DOUBLE = 0.0;

	public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];

	public static final float DEFAULT_FLOAT = 0;

	public static final float[] DEFAULT_FLOAT_VALUES = new float[0];

	public static final int DEFAULT_INTEGER = 0;

	public static final int[] DEFAULT_INTEGER_VALUES = new int[0];

	public static final long DEFAULT_LONG = 0;

	public static final long[] DEFAULT_LONG_VALUES = new long[0];

	public static final String[] DEFAULT_STRING_VALUES = new String[0];

	public static final Number DEFAULT_NUMBER = 0;

	public static final Number DEFAULT_OBJECT = null;

	public static final short DEFAULT_SHORT = 0;

	public static final short[] DEFAULT_SHORT_VALUES = new short[0];

	public static final String DEFAULT_STRING = "";

	public static final String DATE_PATTERN = "dd/MM/yyyy";

	public static final DateFormat DATE_FORMAT = new SimpleDateFormat(DATE_PATTERN); 
	
	public static int[] getIntValues(HttpServletRequest req, String param){
		
		String[] str = req.getParameterValues(param);
		
		if(str == null)
			return DEFAULT_INTEGER_VALUES;
		
		int[] intValues = new int[str.length];
		
		for (int i = 0; i < str.length; i++) {
			intValues[i] = getInt(str[i]);
		}
		
		return intValues;
	}
	
	public static long[] getLongValues(HttpServletRequest req, String param){
		
		String[] str = req.getParameterValues(param);
		
		if(str == null)
			return DEFAULT_LONG_VALUES;
		
		long[] longValues = new long[str.length];
		
		for (int i = 0; i < str.length; i++) {
			longValues[i] = getLong(str[i]);
		}
		
		return longValues;
	}
	
	public static Date[] getDateValues(HttpServletRequest req, String param) throws ParseException{
		
		String[] str = req.getParameterValues(param);
		
		if(str == null)
			return DEFAULT_DATE_VALUES;
		
		Date[] dateValues = new Date[str.length];
		
		for (int i = 0; i < str.length; i++) {
			dateValues[i] = getDate(str[i]);
		}
		
		return dateValues;
	}
	
	
	
	public static String[] getStringValues(HttpServletRequest req, String param){
		
		String[] stringValues = req.getParameterValues(param);
		
		if(stringValues == null)
			return DEFAULT_STRING_VALUES;
		
		return stringValues;
	}
	
	public static String getString(HttpServletRequest req, String param){
		return getString(req.getParameter(param));
	}

	public static String getString(String str){
		
		if(str == null)
			return DEFAULT_STRING;
		
		return String.valueOf(str);
	}
	
	public static int getInt(HttpServletRequest req, String param){
		return getInt(req.getParameter(param));
	}
	
	public static int getInt(String str){
		
		if(str == null)
			return DEFAULT_INTEGER;
		
		return Integer.valueOf(str);
	}
	
	public static long getLong(HttpServletRequest req, String param){
		return getLong(req.getParameter(param));
	}
	
	public static long getLong(String str){
		
		if(str == null)
			return DEFAULT_LONG;
		
		return Long.valueOf(str);
	}
	
	public static Date getDate(HttpServletRequest req, String param) throws ParseException{
		return getDate(req.getParameter(param));
	}
	
	public static Date getDate(String srt) throws ParseException{
		if(srt == null)
			return null;
		return DATE_FORMAT.parse(srt);
	}
	
}